/****
 * AOC starter
 * Copyright (c) 2010, 02nz
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Latest version aviliable through the world-wide-web at the following url:
 * http://code.google.com/p/aoc-starter/
 ****/

#include <windows.h>
#include "ui_menu.h"

#pragma warning(disable: 4244)

CMenuMan gMenu;

CMenu *CMenuMan::Create()
{
	return Add(CreatePopupMenu());
}

CMenu *CMenuMan::Add(HMENU hMenu)
{
	return new CMenu(hMenu);
}

//-------------------------------

CMenu::CMenu(HMENU a)
	:	_Menu(a)
	,	_Count(0)
{
}

VOID CMenu::SetDefault(UINT_PTR uID)
{
	SetMenuDefaultItem(_Menu, uID, 0);
}

BOOL CMenu::Enable(UINT_PTR uID, BOOL Enable)
{
	MENUITEMINFO mii;
	mii.cbSize = sizeof(mii);
	mii.fMask = MIIM_STATE;
	mii.fState = Enable ? MFS_ENABLED : MFS_GRAYED;

	return SetMenuItemInfo(_Menu, uID, FALSE, &mii);
}

BOOL CMenu::Check(UINT_PTR uID, BOOL Check)
{
	MENUITEMINFO mii;
	mii.cbSize = sizeof(mii);
	mii.fMask = MIIM_STATE;
	mii.fState = Check ? MF_CHECKED : MF_UNCHECKED;

	return SetMenuItemInfo(_Menu, uID, FALSE, &mii);
}

LONG CMenu::Append(UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem, HICON hIcon, PCSTR lpNewItemA)
{
	LONG id = lpNewItemA
		? AppendMenuA(_Menu, uFlags, uIDNewItem, lpNewItemA)
		: AppendMenu(_Menu, uFlags, uIDNewItem, lpNewItem);

	TMenuData *pd = &_List[_Count++];
	pd->Icon = hIcon;
	pd->ID = uIDNewItem;
	pd->Parent = this;

	MENUITEMINFO mii;
	mii.cbSize = sizeof(mii);
	mii.fMask = MIIM_BITMAP | MIIM_DATA;
	mii.dwItemData = (ULONG_PTR)pd;
	mii.hbmpItem = HBMMENU_CALLBACK;

	if(uFlags & MF_POPUP)
	{
		mii.hSubMenu = (HMENU)uIDNewItem;
		mii.fMask |= MIIM_SUBMENU;
	}

	SetMenuItemInfo(_Menu, uIDNewItem, FALSE, &mii);
	return id;
}

VOID CMenu::Release()
{
	DestroyMenu(_Menu);
	delete this;
}

BOOL CMenu::Track(int x, int y, HWND hWnd, UINT uFlags, CONST RECT *prcRect)
{
	SetForegroundWindow(hWnd);
	return TrackPopupMenu(_Menu, uFlags, x, y, 0, hWnd, prcRect);
}

//-------------------------------

VOID CMenuMan::OnMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_MEASUREITEM:
		if(	((LPMEASUREITEMSTRUCT)lParam)->CtlType == ODT_MENU &&
			((LPMEASUREITEMSTRUCT)lParam)->itemData )
		{
			OnMeasure((LPMEASUREITEMSTRUCT)lParam);
		}
		break;

	case WM_DRAWITEM:
		if(	((PDRAWITEMSTRUCT)lParam)->CtlType == ODT_MENU &&
			((PDRAWITEMSTRUCT)lParam)->itemData )
		{
			OnDraw((PDRAWITEMSTRUCT)lParam);
		}
		break;
	}
}

VOID CMenuMan::OnMeasure(LPMEASUREITEMSTRUCT pmis)
{
	CMenu::TMenuData *pd = (CMenu::TMenuData *)pmis->itemData;
	if(!pd)
		return;

	LONG icoW = GetSystemMetrics(SM_CXSMICON);
	LONG icoH = GetSystemMetrics(SM_CYSMICON);

	LONG lw = icoW - GetSystemMetrics(SM_CXMENUCHECK) + 6;

	pmis->itemWidth = max(0, lw);
	pmis->itemHeight = icoH + 2;
}

static
BOOL CALLBACK _DrawStateProc(HDC hdc, LPARAM lData, WPARAM wData, int cx, int cy)
{
	return DrawIconEx(hdc, 0, 0, (HICON)lData, cx, cy, 0, 0, DI_NORMAL);
}

VOID CMenuMan::OnDraw(LPDRAWITEMSTRUCT pdis)
{
	CMenu::TMenuData *pd = (CMenu::TMenuData *)pdis->itemData;
	if(!pd || !pd->Icon)
		return;

	LONG icoW = GetSystemMetrics(SM_CXSMICON);
	LONG icoH = GetSystemMetrics(SM_CYSMICON);

	LONG y = (pdis->rcItem.bottom - pdis->rcItem.top - icoH) >> 1;
	y++;

	if(!(pdis->itemState & ODS_SELECTED))
	{
		if(pdis->itemState & ODS_CHECKED)
		{
			RECT rce;
			rce.left = 1;
			rce.right = rce.left + icoW + 3;
			rce.top = y - 2;
			rce.bottom = rce.top + icoH + 4;
			//FillRect(pdis->hDC, &rce, GetSysColorBrush(COLOR_MENU));
			DrawEdge(pdis->hDC, &rce, BDR_SUNKENOUTER, BF_RECT);
		}
	}

	DrawState(pdis->hDC, NULL, _DrawStateProc, (LPARAM)pd->Icon, 0, 3, y, icoW, icoH,
		(pdis->itemState & ODS_DISABLED) ? (DST_COMPLEX|DSS_DISABLED) : (DST_COMPLEX|DSS_NORMAL) );


}

